package com.example.service;

import com.alibaba.trade.param.AlibabaAscpTradeModelProductItemInfo;
import com.alibaba.trade.param.AlibabaAscpTradeModelTradeInfo;
import com.alibaba.trade.param.AlibabaAscpTradeModelOrderBaseInfo;
import com.alibaba.trade.param.AlibabaAscpTradeTradeContact;
import com.alibaba.trade.param.AlibabaAscpTradeSkuItemDesc;
import com.alibaba.trade.param.ProducItemExtendFields;
import com.example.shiti.ProductItemInfo;
import com.example.shiti.TradeInfo;

import java.util.ArrayList;
import java.util.List;

public class TradeInfoToAlibabaAscpTradeModelTradeInfoConverter {

    public static AlibabaAscpTradeModelTradeInfo convert(TradeInfo source) {
        AlibabaAscpTradeModelTradeInfo alibabaTradeInfo = new AlibabaAscpTradeModelTradeInfo();

        // 创建并设置订单基础信息
        AlibabaAscpTradeModelOrderBaseInfo baseInfo = new AlibabaAscpTradeModelOrderBaseInfo();
        baseInfo.setCurrency(source.getCurrency());
        baseInfo.setId(source.getId());
        baseInfo.setModifyTime(source.getModifyTime());
        baseInfo.setCreateTime(source.getCreateTime());
        baseInfo.setPayTime(source.getPayTime());
        baseInfo.setReceivingTime(source.getReceivingTime());
        baseInfo.setAllDeliveredTime(source.getAllDeliveredTime());
        baseInfo.setCompleteTime(source.getCompleteTime());

        // 设置买家联系人信息
        AlibabaAscpTradeTradeContact buyerContact = new AlibabaAscpTradeTradeContact();
        buyerContact.setName(source.getBuyerContactName());
        buyerContact.setMobile(source.getBuyerContactMobile());
        buyerContact.setPhone(source.getBuyerContactPhone());
        buyerContact.setProvince(source.getBuyerContactProvince());
        buyerContact.setCity(source.getBuyerContactCity());
        buyerContact.setArea(source.getBuyerContactArea());
        buyerContact.setTown(source.getBuyerContactTown());
        buyerContact.setAddress(source.getBuyerContactAddress());
        buyerContact.setZip(source.getBuyerContactZip());
        buyerContact.setCaid(source.getCaid());
        buyerContact.setOaid(source.getOaid());
        baseInfo.setBuyerContact(buyerContact);

        // 设置交易状态和退款状态
        baseInfo.setStatus(source.getStatus());
        baseInfo.setRefundStatus(source.getRefundStatus());
        baseInfo.setRemark(source.getRemark());

        // 设置供应商信息
        baseInfo.setAscpSupplierId(source.getAscpSupplierId());
        baseInfo.setAscpSupplierName(source.getAscpSupplierName());

        // 设置金额信息
        baseInfo.setShippingFee(source.getShippingFee());
        baseInfo.setTotalAmount(source.getTotalAmount());

        // 将订单基础信息设置到最终的 TradeInfo 对象
        alibabaTradeInfo.setBaseInfo(baseInfo);

        // 设置商品条目信息
        List<AlibabaAscpTradeModelProductItemInfo> productItems = new ArrayList<>();
        for (ProductItemInfo item : source.getProductItems()) {
            AlibabaAscpTradeModelProductItemInfo productItemInfo = new AlibabaAscpTradeModelProductItemInfo();
            productItemInfo.setSubItemID(item.getSubItemID());
            productItemInfo.setProductID(item.getProductID());
            productItemInfo.setSkuID(item.getSkuID());
            productItemInfo.setName(item.getName());
            productItemInfo.setQuantity(item.getQuantity());
            String[] imgs = item.getProductImgUrl().split(",");
            List<String> ss = new ArrayList<>();
            for(String s :imgs){
                ss.add(s.substring(1, s.length() - 1));
            }
            productItemInfo.setProductImgUrl(ss.toArray(new String[0])); // Assuming productImgUrl is a comma-separated string
            productItemInfo.setStatus(item.getStatus());
            productItemInfo.setRefundId(item.getRefundId());
            productItemInfo.setRefundStatus(item.getRefundStatus());
            productItemInfo.setItemAmount(item.getItemAmount());
            productItemInfo.setCargoNumber(item.getCargoNumber());
            productItemInfo.setProductCargoNumber(item.getProductCargoNumber());

            // 设置 SKU 信息
            List<AlibabaAscpTradeSkuItemDesc> skuInfos = new ArrayList<>();
            String[] skuNames = item.getSkuInfosName().split(",");
            String[] skuValues = item.getSkuInfosValue().split(",");
            for (int i = 1; i < skuNames.length; i++) {
                AlibabaAscpTradeSkuItemDesc skuItemDesc = new AlibabaAscpTradeSkuItemDesc();
                skuItemDesc.setName(skuNames[i]);
                skuItemDesc.setValue(skuValues[i-1]);
                skuInfos.add(skuItemDesc);
            }
            productItemInfo.setSkuInfos(skuInfos.toArray(new AlibabaAscpTradeSkuItemDesc[0]));

            // 设置扩展字段信息
            List<ProducItemExtendFields> extendsFields = new ArrayList<>();
            String[] fieldKeys = item.getProductItemExtendFieldsKey().split(",");
            String[] fieldValues = item.getProductItemExtendFieldsValue().split(",");
            for (int i = 0; i < fieldKeys.length; i++) {
                ProducItemExtendFields extendField = new ProducItemExtendFields();
                extendField.setKey(fieldKeys[i]);
                extendField.setValue(fieldValues[i]);
                extendsFields.add(extendField);
            }
            productItemInfo.setProductItemExtendsFileds(extendsFields.toArray(new ProducItemExtendFields[0]));

            productItems.add(productItemInfo);
        }

        // 设置商品条目到 alibabaTradeInfo
        alibabaTradeInfo.setProductItems(productItems.toArray(new AlibabaAscpTradeModelProductItemInfo[0]));

        return alibabaTradeInfo;
    }
}
